import pdf from "vue-pdf";
import { mapState } from "vuex";
import {
    uploadContractFile,
    addRegistration
} from "~/views/wy-asset-management/registration/api/registration.js";

export default {
    name: "ocr-add",
    components: { pdf },
    data () {
        /**
         * async / await 同步
         * 校验申请单位的唯一性
         */
        let validatorProjectName = async (rule, value, callback) => {
            if (value == "") {
                callback(new Error("请输入项目名称"));
            }
            await validatorProjectNameByAdd(value).then(respData => {
                let result = respData.data.data;
                if (result == false) {
                    callback(new Error("项目名称重复"));
                }
            });
        };

        return {
            showWin: false,
            dialogVisible: false,
            roomFile: null,
            pdfUrl: "",
            contractUrl: "",
            dialogContractVisible: false,

            formObj: {
                //基本信息
                assetRegistrationId: "",
                projectName: "小米科技有限公司",
                registrationUnit: "小米科技有限公司",
                registerDate: "",
                constructionUnit: "小米科技",
                constructionAddress: "北京中关村",
                approvalDocumentNo: "XM33234464",
                constructionStartTime: "2018-10-31",
                completedTime: "2020-10-31",
                finalAccountsDocumentNo: "JS231556",
                assetOccupyingUnit: "小米生态",
                superiorCompetentUnit: "小米集团",
                assetUseUnit: "小米科技",
                fixedAssets: 1000000,
                currentAssets: 1000000,
                intangibleAssets: 1000000,
                deferredAssets: 1000000,
                otherAssets: 1000000,
                stayWriteOffExpenditure: 1000000,
                total: 6000000,
                remarks: "",
                filledBy: "张三风",
                reviewer: "李逍遥",
                personInCharge: "肖秋风",
                //附件
                fileList: [],
                useCardPropertyList: [],
                propertyIdList: []
            },
            rules: {
                projectName: [
                    {
                        required: true,
                        message: "请输入项目名称",
                        trigger: "blur"
                    },
                    { validator: validatorProjectName, trigger: "blur" }
                ],
                registrationUnit: [
                    {
                        required: true,
                        message: "请输入登记单位",
                        trigger: "blur"
                    }
                ],
                registerDate: [
                    {
                        required: true,
                        message: "请输入填表日期",
                        trigger: "blur"
                    }
                ],
                constructionUnit: [
                    {
                        required: true,
                        message: "请输入建设单位",
                        trigger: "blur"
                    }
                ],
                constructionAddress: [
                    {
                        required: true,
                        message: "请输入项目建设地址",
                        trigger: "blur"
                    }
                ],
                approvalDocumentNo: [
                    {
                        required: true,
                        message: "请输入设计立项文号",
                        trigger: "blur"
                    }
                ],
                constructionStartTime: [
                    {
                        required: true,
                        message: "请输入建设开始时间",
                        trigger: "blur"
                    }
                ],
                completedTime: [
                    {
                        required: true,
                        message: "请输入竣工时间",
                        trigger: "blur"
                    }
                ],
                finalAccountsDocumentNo: [
                    {
                        required: true,
                        message: "请输入决算批复文号",
                        trigger: "blur"
                    }
                ],
                assetOccupyingUnit: [
                    {
                        required: true,
                        message: "请输入资产占用单位",
                        trigger: "blur"
                    }
                ],
                superiorCompetentUnit: [
                    {
                        required: true,
                        message: "请输入上级主管单位",
                        trigger: "blur"
                    }
                ],
                assetUseUnit: [
                    {
                        required: true,
                        message: "请输入资产使用单位",
                        trigger: "blur"
                    }
                ],
                filledBy: [
                    { required: true, message: "请输入填报人", trigger: "blur" }
                ],
                reviewer: [
                    { required: true, message: "请输入复核人", trigger: "blur" }
                ],
                personInCharge: [
                    {
                        required: true,
                        message: "请输入单位负责人",
                        trigger: "blur"
                    }
                ],
                //金额校验
                fixedAssets: [
                    {
                        required: true,
                        message: "请输入金额",
                        trigger: "change"
                    },
                    {
                        pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
                        message: "请输入合法的金额数字，最多两位小数",
                        trigger: "change"
                    }
                ],
                currentAssets: [
                    {
                        required: true,
                        message: "请输入金额",
                        trigger: "change"
                    },
                    {
                        pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
                        message: "请输入合法的金额数字，最多两位小数",
                        trigger: "change"
                    }
                ],
                intangibleAssets: [
                    {
                        required: true,
                        message: "请输入金额",
                        trigger: "change"
                    },
                    {
                        pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
                        message: "请输入合法的金额数字，最多两位小数",
                        trigger: "change"
                    }
                ],
                deferredAssets: [
                    {
                        required: true,
                        message: "请输入金额",
                        trigger: "change"
                    },
                    {
                        pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
                        message: "请输入合法的金额数字，最多两位小数",
                        trigger: "change"
                    }
                ],
                otherAssets: [
                    {
                        required: true,
                        message: "请输入金额",
                        trigger: "change"
                    },
                    {
                        pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
                        message: "请输入合法的金额数字，最多两位小数",
                        trigger: "change"
                    }
                ],
                stayWriteOffExpenditure: [
                    {
                        required: true,
                        message: "请输入金额",
                        trigger: "change"
                    },
                    {
                        pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
                        message: "请输入合法的金额数字，最多两位小数",
                        trigger: "change"
                    }
                ],
                total: [
                    {
                        required: true,
                        message: "请输入金额",
                        trigger: "change"
                    },
                    {
                        pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
                        message: "请输入合法的金额数字，最多两位小数",
                        trigger: "change"
                    }
                ]
            }
        };
    },
    mounted () {
        let date = new Date();
        let year = date.getFullYear(); // 年
        let month = date.getMonth() + 1; // 月
        let day = date.getDate(); // 日
        this.formObj.registerDate =
            year + "-" + month + "-" + day + " 00:00:00";
    },
    methods: {
        /**
         * 计算合计金额
         */
        calculationTotal () {
            this.formObj.total =
                parseFloat(this.formObj.fixedAssets) +
                parseFloat(this.formObj.currentAssets) +
                parseFloat(this.formObj.intangibleAssets) +
                parseFloat(this.formObj.deferredAssets) +
                parseFloat(this.formObj.otherAssets) +
                parseFloat(this.formObj.stayWriteOffExpenditure);

        },
        show () {
            this.dialogVisible = true;
        },
        handleClose (done) {
            this.$confirm("确认关闭？").then(_ => {
                done();
            });
        },
        // 识别文件
        getFile (event) {
            this.roomFile = event.target.files[0];
        },

        //上传附件
        upload () {
            if (this.roomFile == null) {
                this.$alert("请先选择文件，再上传");
                return;
            }

            let formData = new FormData();
            formData.append("file", this.roomFile);

            uploadContractFile(formData).then(response => {
                if (response.data.code == 0) {
                    this.$alert("上传附件成功");
                    this.pdfUrl =
                        window.location.origin + response.data.data.url;
                    this.contractUrl = response.data.data.url;
                    console.log();
                } else {
                    this.$alert("服务端异常：" + response.message);
                }
            });
        },
        read () {
            this.showWin = true;
        },
        ok () {
            this.$refs["ruleForm"].validate(valid => {
                if (valid) {
                    let formData = this.formObj;
                    addRegistration(formData)
                        .then(data => {
                            this.$message({
                                type: "success",
                                message: "操作成功"
                            });
                            //操作完成 跳转页面
                            this.$router.push(
                                "/asset-management/registration-list"
                            );
                        })
                        .catch(error => {
                            this.$message({
                                type: "error",
                                message: "操作失败"
                            });
                        })
                        .finally(() => (this.loading = false));
                } else {
                    return false;
                }
            });
        },
        close () {
            this.dialogVisible = false;
        }
    }
};
